<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>抽奖号码管理</title>
    <link rel="stylesheet" href="css/reset.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f5f5f5;
            padding: 20px;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .input-section {
            margin-bottom: 20px;
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }
        .input-section input[type="number"] {
            width: 120px;
            padding: 8px;
            margin-right: 0;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
        .input-section input#excludeNumber {
            width: 300px;
        }
        .input-section button {
            padding: 8px 15px;
            background: #dc3545;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .input-section button:hover {
            background: #c82333;
        }
        .numbers-display {
            margin-top: 20px;
        }
        .numbers-display h2 {
            color: #666;
            margin-bottom: 10px;
        }
        .numbers-list {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
        }
        .number-item {
            background: #f8f9fa;
            padding: 5px 10px;
            border-radius: 4px;
            border: 1px solid #dee2e6;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        .number-item.selected {
            background: #dc3545;
            color: white;
            border-color: #dc3545;
            transform: translateY(-2px);
            box-shadow: 0 2px 4px rgba(0,0,0,0.2);
        }

        .number-item:hover {
            background: #e9ecef;
            transform: translateY(-2px);
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .error-message {
            color: #dc3545;
            margin-top: 10px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>抽奖号码管理</h1>
        
        <div class="input-section">
            <input type="number" id="totalCount" min="1" max="999" placeholder="抽奖人数">
            <button onclick="generateNumbers()">生成号码表</button>
            <input type="string" id="excludeNumber" min="1" max="999" placeholder="输入号码">
            <button onclick="excludeNumber()">排除号码</button>
            <div class="error-message" id="errorMessage"></div>
        </div>

        <div class="numbers-display">
            <h2>当前可用号码列表：</h2>
            <div class="numbers-list" id="numbersList"></div>
        </div>
    </div>

    <script>
        // 页面加载时显示当前可用号码
        document.addEventListener('DOMContentLoaded', function() {
            updateNumbersList();
        });

        // 更新号码列表显示
        function updateNumbersList() {
            const numbersList = document.getElementById('numbersList');
            const availableNumbers = JSON.parse(localStorage.getItem('availableNumbers')) || [];
            
            // 排序号码
            availableNumbers.sort((a, b) => a - b);
            
            // 清空现有显示
            numbersList.innerHTML = '';
            
            // 添加所有号码
            availableNumbers.forEach(number => {
                const numberElement = document.createElement('div');
                numberElement.className = 'number-item';
                numberElement.textContent = number < 10 ? '0' + number : number;
                // 添加点击事件
                numberElement.addEventListener('click', function() {
                    const input = document.getElementById('excludeNumber');
                    const currentNumbers = input.value ? input.value.split(',').map(n => parseInt(n.trim())).filter(n => !isNaN(n)) : [];
                    
                    if (currentNumbers.includes(number)) {
                        // 如果号码已经在选中列表中，则移除它
                        const newNumbers = currentNumbers.filter(n => n !== number);
                        input.value = newNumbers.length > 0 ? newNumbers.sort((a, b) => a - b).join(', ') : '';
                        numberElement.classList.remove('selected');
                    } else {
                        // 如果号码不在选中列表中，则添加它
                        currentNumbers.push(number);
                        input.value = currentNumbers.sort((a, b) => a - b).join(', ');
                        numberElement.classList.add('selected');
                    }
                });
                numbersList.appendChild(numberElement);
            });
        }

        // 生成号码表
        function generateNumbers() {
            const input = document.getElementById('totalCount');
            const count = parseInt(input.value);

            // 验证输入
            if (!count || count < 1 || count > 999) {
                showError('请输入1-999之间的有效人数');
                return;
            }

            // 生成号码表
            const numbers = Array.from({length: count}, (_, i) => i + 1);
            
            // 更新localStorage
            localStorage.setItem('availableNumbers', JSON.stringify(numbers));

            // 清空输入并更新显示
            input.value = '';
            errorMessage.style.display = 'none';
            updateNumbersList();
        }

        // 排除号码
        function excludeNumber() {
            const input = document.getElementById('excludeNumber');
            const errorMessage = document.getElementById('errorMessage');
            const numbers = input.value.split(',').map(n => parseInt(n.trim())).filter(n => !isNaN(n));

            // 验证输入
            if (numbers.length === 0) {
                showError('请选择要排除的号码');
                return;
            }

            for (const number of numbers) {
                if (number < 1 || number > 999) {
                    showError('号码必须在1-999之间');
                    return;
                }
            }

            // 获取当前可用号码
            let availableNumbers = JSON.parse(localStorage.getItem('availableNumbers')) || [];

            // 检查号码是否都存在
            for (const number of numbers) {
                if (!availableNumbers.includes(number)) {
                    showError('号码 ' + number + ' 不在可用号码列表中');
                    return;
                }
            }

            // 移除所有选中的号码
            availableNumbers = availableNumbers.filter(n => !numbers.includes(n));

            // 更新localStorage
            localStorage.setItem('availableNumbers', JSON.stringify(availableNumbers));

            // 清空输入并更新显示
            input.value = '';
            errorMessage.style.display = 'none';
            updateNumbersList();
        }

        // 显示错误信息
        function showError(message) {
            const errorMessage = document.getElementById('errorMessage');
            errorMessage.textContent = message;
            errorMessage.style.display = 'block';
        }
    </script>
</body>
</html>